#!/usr/bin/env bash

# docs: https://docs.docker.com/engine/install/centos/
# docs: https://docs.docker.com/engine/install/debian/

install_rpm() {
    sudo yum remove \
        --assumeyes \
        docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
    sudo yum install -y yum-utils
    sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    sudo yum install \
        --assumeyes \
        docker-ce-20.10.23 docker-ce-cli-20.10.23 docker-compose-plugin-2.10.2
    # containerd.io-1.6.15
    # docker-compose-plugin-2.9.0-6413
    sudo systemctl start docker
}

install_deb() {
    sudo apt purge --yes \
        docker.io docker-doc docker-compose podman-docker containerd runc
    echo "Downloading containerd.io_1.6.33-1_amd64.deb"
    curl -o ~/data/containerd.io_1.6.33-1_amd64.deb https://mirrors.aliyun.com/docker-ce/linux/debian/dists/bookworm/pool/stable/amd64/containerd.io_1.6.33-1_amd64.deb
    echo "Downloading docker-ce-cli_26.1.4-1~debian.12~bookworm_amd64.deb"
    curl -o ~/data/docker-ce-cli_26.1.4-1~debian.12~bookworm_amd64.deb https://mirrors.aliyun.com/docker-ce/linux/debian/dists/bookworm/pool/stable/amd64/docker-ce-cli_26.1.4-1~debian.12~bookworm_amd64.deb
    echo "Downloading docker-ce_26.1.4-1~debian.12~bookworm_amd64.deb"
    curl -o ~/data/docker-ce_26.1.4-1~debian.12~bookworm_amd64.deb https://mirrors.aliyun.com/docker-ce/linux/debian/dists/bookworm/pool/stable/amd64/docker-ce_26.1.4-1~debian.12~bookworm_amd64.deb
    echo "Downloading docker-compose-plugin_2.27.1-1~debian.12~bookworm_amd64.deb"
    curl -o ~/data/docker-compose-plugin_2.27.1-1~debian.12~bookworm_amd64.deb https://mirrors.aliyun.com/docker-ce/linux/debian/dists/bookworm/pool/stable/amd64/docker-compose-plugin_2.27.1-1~debian.12~bookworm_amd64.deb
    sudo dpkg --install ~/data/containerd.io_1.6.33-1_amd64.deb
    sudo dpkg --install ~/data/docker-ce-cli_26.1.4-1~debian.12~bookworm_amd64.deb
    sudo dpkg --install ~/data/docker-ce_26.1.4-1~debian.12~bookworm_amd64.deb
    sudo dpkg --install ~/data/docker-compose-plugin_2.27.1-1~debian.12~bookworm_amd64.deb
    sudo service docker start
}

version() {
    # docker version
    echo -e "\033[32m"
    echo -e "=============================="
    echo -e "  Docker: v$(docker --version | awk '{print $3}')"
    echo -e "=============================="
    echo -e "\033[0m"
}

create_data() {
    if [ ! -d "$HOME/data" ]; then
        mkdir -p "$HOME/data"
    fi
}

main() {
    create_data
    if grep -qi "anolis" /etc/os-release; then
        echo -e "  Anolis OS"
        install_rpm
    elif grep -qi "debian" /etc/os-release; then
        echo -e "  Debian"
        install_deb
    else
        echo -e "  Unknown OS"
        exit 1
    fi
    version
}

main
